﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using DataAccess.FinancialManage;
using Ninject;
using WinForm.Helper;

namespace WinForm.SystemManage
{
    public partial class HandleAssetConfirm : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IFinancial _financial = Kernel.Get<Financial>();

        private jsh_property selfP = null;

        /// <summary>
        /// 是否是退出系统
        /// </summary>
        private bool isExit = false;

        public HandleAssetConfirm()
        {
            InitializeComponent();

            try
            {
                ResultInfo<jsh_property> jshProperty = _financial.GetTopProperty(SystemInfo.OperateAddressId);
                if (jshProperty.ResultNumber == ResultEnum.Success)
                {
                    selfP = jshProperty.Obj;
                    labelX3.Text =
                        (jshProperty.Obj.Currentmoney + jshProperty.Obj.Currentreceiptmoney + jshProperty.Obj.Fixmoney).
                            ToString();
                    labelX4.Text = (jshProperty.Obj.Currentcardcounts + jshProperty.Obj.Fixcards).ToString();
                }
                else
                {
                    labelX3.Text = "0";
                    labelX4.Text = "0";
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxInfo("获取资产确认信息出现错误，请联系管理员！");
            }
        }

        public HandleAssetConfirm(bool isExit)
        {
            this.isExit = isExit;

            InitializeComponent();

            try
            {
                ResultInfo<jsh_property> jshProperty = _financial.GetTopProperty(SystemInfo.OperateAddressId);
                if (jshProperty.ResultNumber == ResultEnum.Success)
                {
                    selfP = jshProperty.Obj;
                    labelX3.Text =
                        (jshProperty.Obj.Currentmoney + jshProperty.Obj.Currentreceiptmoney + jshProperty.Obj.Fixmoney).
                            ToString();
                    labelX4.Text = (jshProperty.Obj.Currentcardcounts + jshProperty.Obj.Fixcards).ToString();
                }
                else
                {
                    labelX3.Text = "0";
                    labelX4.Text = "0";
                }
            }
            catch (Exception)
            {
                MyMessageBox.ShowMessageBoxInfo("获取资产确认信息出现错误，请联系管理员！");
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {

                if (Converts.ConvertToDecimal(textBoxX1.Text) < 0)
                {
                    MyMessageBox.ShowMessageBoxInfo("实际金额不能小于0");
                }
                else
                {
                    if (Converts.ConvertToInt(textBoxX2.Text) < 0)
                    {
                        MyMessageBox.ShowMessageBoxInfo("实际卡数目不能小于0");
                    }
                    else
                    {
                        if (selfP == null)
                        {
                            ResultInfo<jsh_property> p = _financial.GetTopProperty(SystemInfo.OperateAddressId);
                            if (p.ResultNumber == ResultEnum.Success)
                            {
                                selfP = p.Obj;
                            }
                        }

                        decimal currentFixmoney = 0;
                        int currentFixcards = 0;

                        string infomessage = string.Empty;
                        decimal dbmoney = selfP == null ? 0 : (selfP.Currentmoney + selfP.Currentreceiptmoney + selfP.Fixmoney);
                        int dbcards = selfP == null ? 0 : (selfP.Currentcardcounts + selfP.Fixcards);
                        decimal txtmoney = Converts.ConvertToDecimal(textBoxX1.Text);
                        int txtcards = Converts.ConvertToInt(textBoxX2.Text);
                        if (txtcards > dbcards)
                        {
                            infomessage = string.Format("当前站点实际卡数目:{0}张，大于数据库中卡数目:{1}张，卡差额为{2}张。",txtcards,dbcards, txtcards - dbcards);

                        }
                        else if (txtcards == dbcards)
                        {
                            infomessage = string.Format("当前站点实际卡数目:{0}张，等于数据库中卡数目:{1}张，卡无差额。",txtcards,dbcards);
                        }
                        else
                        {
                            infomessage = string.Format("当前站点实际卡数目:{0}张，小于数据库中卡数目:{1}张，卡差额{2}张。",txtcards,dbcards, dbcards - txtcards);
                        }
                        currentFixcards = txtcards - dbcards + (selfP == null ? 0 : selfP.Fixcards);

                        if (txtmoney > dbmoney)
                        {
                            infomessage += string.Format("\r\n当前站点实际钱数目:{0}元，大于数据库中钱数目:{1}，钱差额{2}元。",txtmoney,dbmoney, txtmoney - dbmoney);
                        }
                        else if (txtmoney == dbmoney)
                        {
                            infomessage += string.Format("\r\n当前站点实际钱数目:{0}元，等于数据库中钱数目:{1}，钱无差元。",txtmoney,dbmoney);
                        }
                        else
                        {
                            infomessage += string.Format("\r\n当前站点实际钱数目:{0}元，小于数据库中钱数目:{1}，钱差额{2}元。",txtmoney,dbmoney, dbmoney - txtmoney);
                        }
                        currentFixmoney = txtmoney - dbmoney + (selfP == null ? 0 : selfP.Fixmoney);

                        DialogResult dr = MyMessageBox.ShowMessageBoxQuestion(infomessage + "\r\n是否保存？");
                        if (dr == DialogResult.Yes)
                        {
                            ResultInfo<int> op = _financial.CreateProperty(0, 0, 0, 0, 0, 0, currentFixmoney, currentFixcards,
                                                                      infomessage);
                            if (op.ResultNumber == ResultEnum.Success && op.Obj > 0)
                            {
                                if (!this.isExit)
                                {
                                    Visible = false;
                                    //显示主界面
                                    var frmNew = new FrmNew { StartPosition = FormStartPosition.CenterScreen };

                                    frmNew.ShowDialog();
                                }
                                Close();
                                Dispose();
                            }
                            else
                            {
                                MyMessageBox.ShowMessageBoxInfo(op.Describe);
                            }
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                MyMessageBox.ShowMessageBoxInfo(e1.Message);
            }
        }
    }
}
